

import java.sql.*;
import java.util.Calendar;
import javax.swing.JOptionPane;


public class Survey {
    public int studentID;
    public String name;
    public Calendar dateSubmitted;
    public int year;
    public Calendar bedtime;
    public int neatness;
    public String comment;

    private Connection connect;

    /*
    public Survey(int studentID, Calendar bedtime, int neatness, String comment) {
        this.studentID = studentID;
        this.bedtime = bedtime;
        this.neatness = neatness;
        this.comment = comment;
    }
     * 
     */
    
    public Survey(int studentID, Connection connect){
        this.connect = connect;
        this.studentID = studentID;
        loadFromDB();
    }
    
    
    public void update(){
        try {
            String updateQuery = "UPDATE survey SET dateSubmitted = ?, year = ?,"
                    + "bedtime = ?, neatness = ?, comment = ? "
                    + "WHERE studentID = ?";
            /*
            PreparedStatement updateStmt = connect.prepareStatement(updateQuery);
            
            updateStmt.setString(1, password);
            updateStmt.setString(2, firstName);
            updateStmt.setString(3, lastName);
            updateStmt.setInt(4, gender);
            updateStmt.setInt(5, gradClass);
            updateStmt.setInt(6, status);
            updateStmt.setInt(7, studentID);
            updateStmt.executeUpdate();
*/                
        }catch (Exception ex){
            JOptionPane.showMessageDialog(null,
                    "Unable to update to database");
             
        }
    }
    
    
    
    
    //Private methods start here
    
    private void loadFromDB(){
        try {
            String loadQuery = "select * from survey where studentID = ?";
            String loadQuery2 = "select firstName, lastName from student where studentID = ?";
            PreparedStatement loadStmt = connect.prepareStatement(loadQuery);
            PreparedStatement loadStmt2 = connect.prepareStatement(loadQuery2);
            loadStmt.setInt(1, studentID);
            loadStmt2.setInt(1, studentID);
            ResultSet surveyInfo = loadStmt.executeQuery();
            if(surveyInfo.next()){
                dateSubmitted = Calendar.getInstance();
                dateSubmitted.setTimeInMillis(surveyInfo.getTimestamp("dateSubmitted").getTime());
                year = surveyInfo.getInt("year");
                bedtime = Calendar.getInstance();
                bedtime.setTimeInMillis(surveyInfo.getTimestamp("bedtime").getTime());
                neatness = surveyInfo.getInt("neatness");
                comment = surveyInfo.getString("comment");
            }
            
            ResultSet nameQuery = loadStmt2.executeQuery();
            if (nameQuery.next())
                name = nameQuery.getString("firstName") + ' ' + nameQuery.getString("lastName");
            
            
        } catch (Exception ex){
            JOptionPane.showMessageDialog(null,
                    "Unable to load survey from database.");
            
        }
        
    }
    @Override
    public String toString() {
        return name;
    }
    
    
    
}
